import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import './medicaltolabel.css'
import { AtCheckbox,AtButton,AtSearchBar} from 'taro-ui'
import { $redirectTo, $navigateBack } from '../../../../global'
import app from '../../../../main.js'

export default class MedicalToLabel extends Component {
 
    config = {
        navigationBarTitleText: '选择病历到标签'
        }
 
    constructor () { 
        super(...arguments); 
        this.state = {
            value: '',
            checkedList: [],
            project_cases:[],
            receive_array:[],
            label_name:this.$router.params.label_name,
            arrId:this.$router.params.arrId,
        }
    }

    componentWillMount () {
        
    }

    componentDidShow() {
        this.allAddToLabel();
    }
    

    allAddToLabel(){
        let me = this;
        let data ={
            offset:0,
            length:1000,
            token:'ssss',
            keyword:me.state.value
        }
        app.get('web/case/history_project_cases',data,function(res){
            let data = res.data;
            console.log('返回结果');
            me.setState({
                project_cases: data.result.list,
                receive_array: JSON.parse(me.$router.params.arr)
            },()=>{
                console.log('compare');
                me.compare();
            })
        })
    }


    //分析比较上个页面传回来的id
    compare(){
        let result=[]
        let receive_array = this.state.receive_array;
        let project_cases = this.state.project_cases;
        for(let i = 0; i < receive_array.length; i++){ 
            for(let j = 0; j < project_cases.length; j++){
                if(receive_array[i].id == project_cases[j].id){
                    result.push(receive_array[i].id)
                    break;
                }
            }
        }
        this.setState({
            checkedList:result,
        })    
    }
    
    
    onChange (value) {
        let me = this
        if(value.length == 0){
            me.setState({
                value: value
            },()=>{
                me.allAddToLabel();
            })  
        }else{
            me.setState({
                value: value
            })
        }
               
    }

    onActionClicks () {
        this.allAddToLabel();
    }

    handleChange (value) {
        this.setState({
          checkedList: value
        },()=>{
        })
    }


    navigateTo() {
        let arr=[];
        for(let val of this.state.checkedList){
            for(let all of this.state.project_cases){
                if(all.id===val){
                    arr.push(all);
                }
            }
        }
        app.redirectTo('../editlabelmedical/editlabelmedical?arr='+JSON.stringify(arr)+'&ids='+JSON.stringify(this.state.checkedList)+'&arrId='+this.state.arrId+'&label_name='+this.state.label_name);
    }  

    render () {
        return (
        <View className='medical-to-label'>
            <AtSearchBar
                value={this.state.value}
                onChange={this.onChange.bind(this)}
                onActionClick={this.onActionClicks.bind(this)}
            />
            <AtCheckbox
                options={
                    this.state.project_cases.map((item)=>{
                        return {
                            value:item.id,
                            label:item.patient_name+'\xa0\xa0\xa0\xa0\xa0\xa0\xa0'+item.created_at,
                            desc:item.diagnosis,         
                        }
                    })
                }
                selectedList={this.state.checkedList}
                onChange={this.handleChange.bind(this)}
            />

            <View className='blank-box'></View>
            <AtButton full={true} className='confirm-btn' onClick={this.navigateTo.bind(this)}>确定</AtButton>

        </View>
        )
    }
}

